<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


//资金流水日志
class Agent extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("AdminAgent");
        parent::__construct();
    $this->agent_parent_id = 1;//管理员顶级
    }


    public function index(){
        $where = [];
        w_s("no_in",true);
        if(isset($this->agent_id)){
            //搜索
            if(!dailiwith($this->agent_id,$this->agents_ids)){ //
                return   failJson("你暂无权限搜索该代理.");
            }
            //非管理员
            $where['a.agent_id'] = $this->agent_id;
        }else{
            if($this->agents_ids=="all"){//超级管理员
                $where['a.agent_id'] =0;
            }else{
                $where['a.id'] =$this->sidRows['agent_id'];
            }
        }
        if(isset($this->tel)){
            $where["a.contacts_tel"] = ['like',"%".$this->tel."%"];
        }
        $res = $this->model->defaultPage($where,[true,false]);

        return   resJson(1,"查询成功",$res['res'],$res['pageInfo']);
    }


    /**
     * @return \think\response\Json  代理商添加
     *
     * 代理商基本配置
     *  过滤元素重复
     */

    public function save(){

        $data = $this->RealData;

        if(isset($data['agent_id'])&&!empty($data['agent_id'])){

            $count = $this->model->baseCount(['id'=>$this->agent_id]);
            if(!$count){
                return   failJson("不存在该代理.");
            }
            if(!dailiwith($data['agent_id'],$this->agents_ids)){
                return   failJson("你暂无权限添加 该代理.");
            }
        }else{
            if($this->agents_ids!="all"){
                return   failJson("非管理员不能添加顶级代理.");
            }
            $data['agent_id'] = 0;
        }
        $this->filterRepetition($this->model,$this,$data);
        $daiLiData = ["auth","kaihui_bank","bank_name","bank_no","contacts_tel","area","cash","commission","agent_name","remark","agent_id","status"];
        $StaffData = ["password"];
        $daiLiDataFilter = filterData($data,$daiLiData);
        $StaffDataFilter = filterData($data,$StaffData);
        $result = $this->model->saveFilterAgent($daiLiDataFilter,$StaffDataFilter);
        if (!$result['status']){
            return failJson("msg");
        }
        return sucessJson($result['msg']);

    }





    /*
     *  修改代理商
     */
    public function editdaili(){
        $data = $this->RealData;
        // dump('1111');exit;


        if(!dailiwith($data['agent_id'],$this->agents_ids)){
            return   failJson("你暂无权限修改.");
        }
        /**
         *  过滤重复
         */
        $this->filterRepetitionEdit($this->model,$this);

        if($this->agents_ids!='all'){
            if(isset($data['agent_id'])){
           /*     if($this->sidRows['agent_id']==$this->agent_id){
                    return   failJson("很抱歉,你无权限修改自身.");
                }*/
                $data['agent_id'] = $this->agent_id;
                if($this->id==$this->agent_id){
                    return   failJson("自身不能归类为自身.");
                }
            }
        }
     $newAuth = new \app\admin\model\Role();
        $newAuthResultCount =  $newAuth->where("id",$this->auth)->count();
        if(empty($newAuthResultCount)){
            return   failJson("你暂无权限.");
        }



        $daiLiData = ["auth","kaihui_bank","bank_name","bank_no","contacts_tel","area","cash","commission","agent_name","remark","id","status"];
        $StaffData = ["password"];

        $daiLiDataFilter = filterData($data,$daiLiData);
        $StaffDataFilter = filterData($data,$StaffData);

        $res = $this->model->Agent_Save_update($daiLiDataFilter,$StaffDataFilter);
        if(!$res['status']){
            return   failJson($res['msg']);
        }
        return sucessJson($res['msg']);
    }






    /**
     *  获取用户能操作的角色
     */
    public function getauthlist(){
        $authList = getAuth($this->sidRows);
        if(empty($authList)){
            return sucessJson("所属角色暂无");
        }
        return sucessJson("获取角色成功",$authList);
    }


    /**
     * @param $model
     * @param $self
     * @param $data 重复数据的过略
     */
    private function filterRepetition($model,$self,$data){
        $countm1 =  $model->baseCount(["contacts_tel" => $self->contacts_tel]);
        $countm2 =  $model->baseCount(["bank_no"=>$self->bank_no]);

        $Countagent_nameCount  = $model->baseCount(["agent_name"=>$self->agent_name]);
        if($countm1>0){
            exit(json_encode(resJson(0,"手机号码重复,请重新输入.")->getData()));

        }
        if($countm2>0){
            exit(json_encode(resJson(0,"银行号码重复,请重新输入.")->getData()));

        }

        if($Countagent_nameCount>0){
            exit(json_encode(resJson(0,"代理商名称重复,请重新输入.")->getData()));
        }
    }

    /**
     * @param $model
     * @param $self
     */
    private function filterRepetitionEdit($model,$self){

        $countm1 =  $model->whereOr(['id'=>$self->id])->whereor(["contacts_tel"=>$self->contacts_tel])->count();
        $countm2 =  $model->whereOr(['id'=>$self->id])->whereOr(["bank_no"=>$self->bank_no])->count();
        $agent_nameCount =  $model->whereOr(['id'=>$self->id])->whereOr(["bank_no"=>$self->agent_name])->count();
        if($countm1>1){

            exit(json_encode(resJson(0,"手机号码重复,请重新输入.")->getData()));
        }
        if($countm2>1){
            exit(json_encode(resJson(0,"银行号码重复,请重新输入.")->getData()));
        }
        if($agent_nameCount>1){
            exit(json_encode(resJson(0,"代理商名称重复,请重新输入.")->getData()));
        }
    }




    /**
     * @return \think\response\Json  栏目数的获取
     */
    public function  tree(){

        //获取当前的tree
        $where = [];

        $res =    $this->model->field("id,agent_id,agent_name as agent_no")->where($where)->select();
        if(empty($res)){
            return   resJson(1,"查询失败");
        }
        foreach($res as $k=>&$v){
            $v = $v->toArray();
        }
        $this->agent_id = $this->sidRows['agent_id'];
        if(isset($this->agent_id)){
            $resd = [];
            if($this->agents_ids!="all"){
                foreach($res as $ks=>$vs){
                    if(in_array($vs['id'],$this->agents_ids)){
                        array_push($resd,$vs);
                    }
                }
                $rsy = [];
                $ress = filter_agent($resd,$this->agent_id,$rsy);
            }else if($this->agents_ids=="all"){
                $rsy =[];
                $ress = filter_agent($res,0,$rsy);
            }


        }else{
            if($this->agents_ids=="all"){
                $rsy =[];
                $ress = filter_agent($res,0,$rsy);
            }else{
                return   resJson(1,"查询失败");
            }

        }
        if(empty($rsy)){
            return   failJson("异常");
        }
        $r = \tool\Tree::makeTreeForHtml($rsy);
        return   sucessJson(1,$r);
    }


}
